package com.hczt.fire.server.backend.sys.service.impl;

import com.hczt.fire.entity.sys.SysRole;
import com.hczt.fire.entity.sys.SysRole_;
import com.hczt.fire.entity.sys.SysUser;
import com.hczt.fire.framework.jpa.repository.IBaseDao;
import com.hczt.fire.framework.jpa.repository.support.PagedQuery;
import com.hczt.fire.framework.jpa.service.IBaseService;
import com.hczt.fire.server.backend.sys.service.SysRoleService;
import com.hczt.fire.server.common.page.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author marvin.ma
 * @create 2017-10-18 22:56
 * @desc 角色管理业务接口实现类
 **/
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private IBaseDao baseDao;

    @Resource
    private IBaseService baseService;

    /**
     * 分页获取角色列表
     * @param pageInfo
     * @return
     */
    @Override
    public PagedQuery<SysRole> getRoleList(PageInfo pageInfo) {
        PagedQuery<SysRole> pagedQuery = new PagedQuery<>();
        pagedQuery.setPageSize(pageInfo.getPageSize());
        pagedQuery.setPageNumber(pageInfo.getPage());
        //pagedQuery.addWhereCondition("id", QueryEnum.EQUAL, 1L);
        pagedQuery = baseDao.findBy(pagedQuery, SysRole.class);

        //QueryCriteria<SysRole, SysRole> criteria = baseDao.getCriteriaQuery(SysRole.class, SysRole.class);
        //List<SysRole> roles = baseDao.findPageListByCriteriaQuery(criteria.getQuery(), pageInfo.getPage(), pageInfo.getPageSize());
        //QueryCriteria<Long, SysRole> countCriteria = baseDao.getCriteriaQuery(Long.class, SysRole.class);
        //countCriteria.getQuery().select(countCriteria.getBuilder().count(countCriteria.getEntity()));
        //long count = baseDao.findCountByCriteriaQuery(countCriteria.getQuery());
        return pagedQuery;
    }

    /**
     * 根据名称查询角色数据
     *
     * @param name
     * @return
     */
    @Override
    public SysRole findByName(String name) {
        return baseDao.findUniqueByProperty(SysRole.class, SysRole_.name.toString(), name);
    }

    /**
     * 查询用户属于什么角色
     *
     * @param userId@return
     */
    @Override
    public List<SysRole> findByUserId(Long userId) {
        List<SysRole> sysRoleList = baseDao.findAll(SysRole.class);
        SysUser sysUser = baseDao.findById(SysUser.class, userId);
        return sysRoleList.stream().map(r -> selectedRole(r, sysUser.getSysRoles())).collect(Collectors.toList());
    }


    private SysRole selectedRole(SysRole sysRole, List<SysRole> userSysRoles) {
        List<SysRole> sysRoles = userSysRoles.stream().filter(r -> r.getId().equals(sysRole.getId())).collect(Collectors.toList());
        sysRole.setIsSelected(!sysRoles.isEmpty());
        return sysRole;
    }

    /**
     * 查找全部角色
     */
    @Override
    public List<SysRole> findAll() {
        return baseDao.findAll(SysRole.class);
    }

    /**
     * 保存角色
     *
     * @param role
     */
    @Override
    public void save(SysRole role) {
        baseDao.save(SysRole.class);
    }

    /**
     * 通过主键查询记录
     *
     * @param id
     * @return
     */
    @Override
    public SysRole findById(Long id) {
        return baseDao.findById(SysRole.class, id);
    }
}
